<script setup lang="ts">
import { reactive, ref } from "vue";
import type { LoginParamsType } from "@/api/apis/qa/rf/LoginPramsType";
import type { FormInstance, FormRules } from "element-plus";
import IconSvg from "@/components/IconSvg/Index.vue";
import { copyRight, appName } from "@/api/apis/qa/dic";
import { sendError } from "@/utils/request";
import { login } from "@/api/apis/qa/login";
import { useRouter } from "vue-router";

const router = useRouter();

const formRef = ref<FormInstance>();
const formData = reactive<LoginParamsType>({
  username: '',
  password: ''
});

const AppName = ref('');
const CopyRightInfo = ref('');

const rules = reactive<FormRules>({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
});

const loading = ref(false);

const onSubmit = async () => {
  if (loading.value === true) {
    return;
  }

  loading.value = true;

  try {
    const valid: boolean | undefined = await formRef.value?.validate();

    if (valid) {
      let result = await login(formData.username, formData.password);

      if (result) {
        const { redirect, ...query } = router.currentRoute.value.query;

        await router.replace({
          path: (redirect as string) || "/",
          query: {
            ...query,
          },
        });
      }
    }
  } catch (error: any) {
    sendError(error);
  }
  loading.value = false;
}

const onRegister = () => {
  // 注册功能实现
  console.log("跳转到注册页面");
}

const InitForm = async () => {
  AppName.value = await appName();
  CopyRightInfo.value = await copyRight();
};

InitForm();
</script>

<template>
  <div class="login-background">
    <div class="login-wrapper">
      <div class="login-card">
        <div class="login-header">
          <div class="logo-container">
            <IconSvg name="logo" class="app-logo"/>
          </div>
          <h1 class="app-title">{{ AppName }}</h1>
          <p class="app-subtitle">质量 · 高效 · 安全 · 便捷</p>
        </div>

        <el-form ref="formRef" :model="formData" :rules="rules" class="login-form">
          <div class="input-group">
            <div class="input-wrapper">
              <IconSvg name="user" class="input-icon"/>
              <el-input
                v-model="formData.username"
                placeholder="请输入用户名"
                clearable
                @keydown.enter="onSubmit"
                class="custom-input"
              />
            </div>
          </div>

          <div class="input-group">
            <div class="input-wrapper">
              <IconSvg name="pwd" class="input-icon"/>
              <el-input
                v-model="formData.password"
                type="password"
                placeholder="请输入密码"
                show-password
                @keydown.enter="onSubmit"
                class="custom-input"
              />
            </div>
          </div>

          <div class="form-options">
            <el-checkbox class="remember-checkbox">记住密码</el-checkbox>
            <!-- <el-button type="text" class="forgot-password">忘记密码?</el-button> -->
          </div>

          <el-button 
            @click="onSubmit" 
            :loading="loading" 
            class="login-button" 
            type="primary"
          >
            登 录
          </el-button>

          <!-- <div class="register-section">
            <span>还没有账号?</span>
            <el-button @click="onRegister" type="text" class="register-link">立即注册</el-button>
          </div> -->
        </el-form>
      </div>

      <div class="footer-info">
        <p class="copyright">{{ CopyRightInfo }}</p>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-background {
  min-height: 100vh;
  background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  position: relative;
  overflow: hidden;

  &::before {
    content: "";
    position: absolute;
    width: 200%;
    height: 200%;
    top: -50%;
    left: -50%;
    background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
    transform: rotate(30deg);
  }

  .login-wrapper {
    width: 100%;
    max-width: 420px;
    position: relative;
    z-index: 2;

    .login-card {
      background: rgba(255, 255, 255, 0.95);
      backdrop-filter: blur(10px);
      border-radius: 20px;
      padding: 50px 40px;
      box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
      border: 1px solid rgba(255, 255, 255, 0.3);

      .login-header {
        text-align: center;
        margin-bottom: 40px;

        .logo-container {
          margin-bottom: 20px;

          .app-logo {
            font-size: 60px;
            color: #409EFF;
          }
        }

        .app-title {
          font-size: 28px;
          font-weight: 600;
          color: #333;
          margin: 0 0 8px;
          letter-spacing: 1px;
        }

        .app-subtitle {
          font-size: 14px;
          color: #666;
          margin: 0;
        }
      }

      .login-form {
        .input-group {
          margin-bottom: 25px;

          .input-wrapper {
            position: relative;
            height: 50px;
            border-radius: 10px;
            background: #f8f9fa;
            border: 1px solid #e2e8f0;
            transition: all 0.3s ease;

            &:hover {
              border-color: #409eff;
              box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.1);
            }

            .input-icon {
              position: absolute;
              left: 15px;
              top: 50%;
              transform: translateY(-50%);
              font-size: 18px;
              color: #94a3b8;
              z-index: 1;
            }

            :deep(.el-input) {
              height: 100%;

              .el-input__wrapper {
                background: transparent;
                box-shadow: none;
                border: none;
                padding-left: 45px;
                height: 100%;
              }

              .el-input__inner {
                height: 100%;
                background: transparent;
                border: none;
                font-size: 15px;
              }
            }
          }
        }

        .form-options {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-bottom: 30px;

          :deep(.el-checkbox__label) {
            color: #64748b;
            font-size: 14px;
          }

          .forgot-password {
            color: #409eff;
            font-size: 14px;
            padding: 0;

            &:hover {
              color: #66b1ff;
            }
          }
        }

        .login-button {
          width: 100%;
          height: 50px;
          border-radius: 10px;
          font-size: 16px;
          font-weight: 500;
          letter-spacing: 2px;
          background: linear-gradient(135deg, #409eff, #64748b);
          border: none;
          margin-bottom: 25px;
          transition: all 0.3s ease;

          &:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(64, 158, 255, 0.3);
          }

          &:active {
            transform: translateY(0);
          }
        }

        .register-section {
          text-align: center;
          font-size: 14px;
          color: #64748b;

          .register-link {
            margin-left: 5px;
            color: #409eff;
            font-weight: 500;
            padding: 0;

            &:hover {
              color: #66b1ff;
            }
          }
        }
      }
    }

    .footer-info {
      text-align: center;
      margin-top: 30px;

      .copyright {
        color: rgba(7, 7, 7, 0.8);
        font-size: 12px;
        margin: 0;
        text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
      }
    }
  }
}

// 响应式设计
@media (max-width: 480px) {
  .login-background {
    padding: 15px;

    .login-wrapper {
      .login-card {
        padding: 40px 25px;

        .login-header {
          .app-title {
            font-size: 24px;
          }

          .app-subtitle {
            font-size: 13px;
          }
        }

        .login-form {
          .input-group {
            margin-bottom: 20px;

            .input-wrapper {
              height: 46px;
            }
          }

          .login-button {
            height: 46px;
            font-size: 15px;
          }
        }
      }
    }
  }
}
</style>